package com.xiaohai.big_event_system.validation;

import com.xiaohai.big_event_system.annotation.State;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StateValidation implements ConstraintValidator<State, String> {
    private String message;

    @Override
    public void initialize(State constraintAnnotation) {
        this.message = constraintAnnotation.message();
    }

    /**
     * @param value   校验的数据
     * @param context
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
        if (value == null) return false;
        return value.equals("已发布") || value.equals("草稿");
    }
}
